# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=guile

pkgbase=mingw-w64-${_realname}1.8
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}1.8"
pkgver=1.8.8
pkgrel=1
pkgdesc="a portable, embeddable Scheme implementation written in C (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'clang32')
url="https://www.gnu.org/software/guile/"
license=('spdx:LGPL-2.1-or-later')
makedepends=(
    "${MINGW_PACKAGE_PREFIX}-autotools"
    "${MINGW_PACKAGE_PREFIX}-cc"
    "texinfo"
)
depends=(
    "${MINGW_PACKAGE_PREFIX}-gc"
    "${MINGW_PACKAGE_PREFIX}-gmp"
    "${MINGW_PACKAGE_PREFIX}-libffi"
    "${MINGW_PACKAGE_PREFIX}-libltdl"
    "${MINGW_PACKAGE_PREFIX}-libsystre"
    "${MINGW_PACKAGE_PREFIX}-libunistring"
    "${MINGW_PACKAGE_PREFIX}-pdcurses"
    "${MINGW_PACKAGE_PREFIX}-readline"
    )
options=('strip' 'staticlibs')
source=("https://ftp.gnu.org/pub/gnu/${_realname}/${_realname}-${pkgver}.tar.gz"
       0003-Fix-the-SRFI-60-copy-bit-documentation.patch
       0004-Define-_GNU_SOURCE-to-fix-the-GNU-kFreeBSD-build.patch
       0005-Don-t-access-uninitialised-memory-in-scm_to_sockaddr.patch
       0006-Make-sockets.test-more-robust.patch
       0007-Mark-Unused-modules-are-removed-gc-test-as-unresolve.patch
       0008-Fix-problems-in-guile-tut.texi-that-cause-makeinfo-t.patch
       0009-Fix-r5rs.texi-makeinfo-failure-by-applying-changes-f.patch
       0010-Fix-test-suite-failure-caused-by-formfeed-difference.patch
       0011-Fix-incompatibility-with-automake-1.13-specify-seria.patch
       0012-remove-spurious-declarations-in-c-tokenize.lex.patch
       0013-Fix-configure-stack-grow-direction-code.patch
       0014-Fix-failed-overflow-detection-with-optimization.patch
       0015-Fix-failed-test-num2integral-and-test-conversion-on-i386.patch
       0016-Avoid-dll-versions.patch
       0017-Try-windows-conversion-of-%load-path-on-MinGW.patch
       0018-Fix-environ-manipulation-for-ucrt-on-MinGW.patch
       0100-Disable-all-failed-tests-for-mingw-w64.patch
)
sha256sums=('c3471fed2e72e5b04ad133bbaaf16369e8360283679bcf19800bc1b381024050'
            '49f4dddb198c109ab992ed09df0352428233f7650c8e01a72c87be14457b2c79'
            'fc76c59ef0a3d237a34fa027e68daea89b7ae4d1d884bb06be653d5e0adb2215'
            '0b2db909ede75996590998c5ce8f71ccf37a22beac3b4afaae1e434ab81a763c'
            '6e6230474e2b32096b806eda8d0be1dc1e4030e707a0df8cf3aa0bec3a489992'
            '5d4ac8600a8b092c8fa837b507650e5abdbd9be8e2bc197c867fc50a4386f7f3'
            '2446c922751a8bf3a0f045adbccc4a9a12e72b267f56f8a384e9f0940cf80e3d'
            '71eccf6ffa799a1740b875f9b92b90a4105c9646c230d7971e641939a3409c42'
            '2b0d75425a9278af7d25740972f34c7f6916be002dd9ecbb8eb21cc215a757b0'
            '31be870c4028d709fee0ceb0fe618e002344db19e632416f4cb2fb54412529b5'
            '5c2314f0c48196e2136d487f232562a7fddb1fcd3144d77fcde10708f86b8bd4'
            'd27f74db6cfea21cf93567b690f5eee4e391f69d9a0f2b3058da7f77806449b5'
            '7765124da929afc713a42241ebfda02b7fd2e0f609834bc493fad532b71ff7d6'
            '6f694960c2e86bb644f1165fd884c9fd84590f4e8e9b5e7911ca59867cd03764'
            '92012bc9934eab7136af4256be76923ba9f628254f2e3b06d165f707e34bb12b'
            '3328af15d213b9b8d7a6688d1b557a360701cc915f45fd05d0f86d244e58311d'
            '0b40e69573d7f41e30b05b2c138f76817da5d871ab4c7a88f9aae7f83154daf6'
            'c5973725758f8d206e48ff4ba17c02ece3fd234664580a2cfe3f02ef928132a5'
	    )

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
# patches from debian for guile-1.8 / 1.8.8+1-10
# https://sources.debian.org/patches/guile-1.8/1.8.8%2B1-10/
  patch -p1 -i "${srcdir}"/0003-Fix-the-SRFI-60-copy-bit-documentation.patch
  patch -p1 -i "${srcdir}"/0004-Define-_GNU_SOURCE-to-fix-the-GNU-kFreeBSD-build.patch
  patch -p1 -i "${srcdir}"/0005-Don-t-access-uninitialised-memory-in-scm_to_sockaddr.patch
  patch -p1 -i "${srcdir}"/0006-Make-sockets.test-more-robust.patch
  patch -p1 -i "${srcdir}"/0007-Mark-Unused-modules-are-removed-gc-test-as-unresolve.patch
  patch -p1 -i "${srcdir}"/0008-Fix-problems-in-guile-tut.texi-that-cause-makeinfo-t.patch
  patch -p1 -i "${srcdir}"/0009-Fix-r5rs.texi-makeinfo-failure-by-applying-changes-f.patch
  patch -p1 -i "${srcdir}"/0010-Fix-test-suite-failure-caused-by-formfeed-difference.patch
# automake-1.13 patch not needed
# patch -p1 -i "${srcdir}"/0011-Fix-incompatibility-with-automake-1.13-specify-seria.patch
  patch -p1 -i "${srcdir}"/0012-remove-spurious-declarations-in-c-tokenize.lex.patch

  patch -p1 -i "${srcdir}"/0013-Fix-configure-stack-grow-direction-code.patch
  patch -p1 -i "${srcdir}"/0014-Fix-failed-overflow-detection-with-optimization.patch
  patch -p1 -i "${srcdir}"/0015-Fix-failed-test-num2integral-and-test-conversion-on-i386.patch

# patches for MinGW
  patch -p1 -i "${srcdir}"/0016-Avoid-dll-versions.patch
  patch -p1 -i "${srcdir}"/0017-Try-windows-conversion-of-%load-path-on-MinGW.patch
  patch -p1 -i "${srcdir}"/0018-Fix-environ-manipulation-for-ucrt-on-MinGW.patch
# disable not passed tests
  patch -p1 -i "${srcdir}"/0100-Disable-all-failed-tests-for-mingw-w64.patch

  autoreconf -fi
}

build() {
  export lt_cv_deplibs_check_method='pass_all'
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared \
    --enable-static \
    --without-threads \
    --enable-error-on-warning=no

  make
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}
# failed checks disabled via 0100-Disable-all-failed-tests-for-mingw-w64.patch
  TMPDIR=C:/temp make check
  echo "****  Running last tests in verbose mode  ****"
  for i in  ../${_realname}-${pkgver}/test-suite/tests/*.test
    do ./check-guile $(basename $i) || true
    done
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  make DESTDIR="${pkgdir}" install

  install -D -m644 "${srcdir}"/${_realname}-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
  install -D -m644 "${srcdir}"/${_realname}-${pkgver}/COPYING.LESSER \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LESSER
}
